<?php
namespace app\tag\model;
use app\common\lib\Paginationm;
use think\Db;
use think\Controller;

class TagsRel extends Controller
{
    protected $beforeActionList = ['start'];
    public function start() {
       $this->itemCategory = 'TagsCategory';
        $this->itemTitle = 'TagsTitle';
        $this->item = 'Tags';
        $this->itemContent = 'TagsContent';
        $this->itemSearch = 'TagsSearch';
        $this->itemSearchRel = 'TagsSearchRel';
        $this->itemType = 'tag';
        $this->itemName = '标签';
        $this->itemModelNameSpace = 'app\tag\model\Tags';
        $this->itemCategoryModelNameSpace = 'app\tag\model\TagsCategory';
    }
    
    public function itemAdd($tagsArray, $itemId,$itemType = null)
    {
        if (!is_array($tagsArray)) {
            return false;
        }
        if (is_array($tagsArray)) {
            db('TagsRel')->where('item_id',$itemId)->delete();
            foreach ($tagsArray as $title) {
            	$title = trim($title);
                if ($title) {
                    $tagInfo = db('Tags')->where('title',$title)->find();
                    if (!$tagInfo) {
                        $result =  getPinyin($title);
                        if ($result) {
                            $tagInfo = db('Tags')->where('url_name',$result)->find();
                            if (!$tagInfo) {
                                db('Tags')->insert(array(
        							'id' => uuid(),
                                    'title' => $title,
                                    'url_name' => $result,
                                ));
                            } else {
                                 db('Tags')->insert(array(
        							'id' => uuid(),
                                    'title' => $title,
                                    'url_name' => $result . $tagInfo['id'],
                                ));
                            }
                        } else {
                            db('Tags')->insert(array(
        						'id' => uuid(),
                                'title' => $title,
                            ));
                        }
                        $tagInfo = db('Tags')->where('title',$title)->find();
                    }
                    db('TagsRel')->insert(array(
                        'tags_id' => $tagInfo['id'],
                        'item_id' => $itemId,
                        'item_type' => $itemType
                    ));
                    $tagsCount = db('TagsRel')->where('tags_id',$tagInfo['id'])->count();
                    if ($tagsCount) {
                        db('Tags')->where('id',$tagInfo['id'])->update(array(
                            'relevance_num' => $tagsCount,
                            'add_time' => time(),
                        ));
                    }
                }
            }
        }
        return true;
    }

    public function getItemTagsStr($indexId)
    {
        $itemList = [];
        $itemTags = db('TagsRel')->where('item_id',$indexId)->select();
        if ($itemTags) {
            foreach ($itemTags as $k => $v) {
                $tagInfo = db('Tags')->where('id',$v['tags_id'])->find();
                if ($tagInfo) {
                    $itemList[] = $tagInfo['title'];
                }
            }
        }
        return $itemList ? implode(',', $itemList) : '';
    }
    
    public function getItemListbyTagIds($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $page = $paramData['page'] ? $paramData['page'] : 1;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'id';
        $order = $paramData['order'] ? $paramData['order'] : 'desc';
        $keywords = $paramData['keywords'] ? $paramData['keywords'] : null;
        $whereArray = $paramData['whereArray'] ? $paramData['whereArray'] : null;
        $tagIds = $paramData['tagIds'] ? $paramData['tagIds'] : null;
        $tagNames = $paramData['tagNames'] ? $paramData['tagNames'] : null;
        $type = $paramData['type'] ? $paramData['type'] : null;
        
        if ($tagNames) {
            $tagNames = explode(',',$tagNames);
            foreach ($tagNames as $val) {
                $tagInfo = db('Tags')->where('title',$val)->find();
                if ($tagInfo) {
                    $tempTagIds[] = $tagInfo['id'];
                }
            }
            $tagIdsWhere['tags_id']  = ['in',$tempTagIds];
        }
        if ($tagIds) {
            $tagIds = explode(',',$tagIds);
            $tagIdsWhere['tags_id']  = ['in',$tagIds];
        }
         
        $itemTagsList = db('TagsRel')->where($tagIdsWhere)->order('id',$order)->page($page,$limit)->select();
        if ($itemTagsList) {
            foreach ($itemTagsList as $k => $v) {
                $itemTagsListIds[] = $v['item_id'];
            }
            $itemTagsListIds = implode(',', $itemTagsListIds);
            $paramData['uuids'] = $itemTagsListIds;
            if ($type) {
          		return model('app\\' . $type . '\\model\\' . ucfirst($type) . 's')->getItemList($paramData);
            }
            return model($this->itemModelNameSpace)->getItemList($paramData);
        }
    }
    
    public function getPaginationbyTagIds($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $page = $paramData['page'] ? $paramData['page'] : 1;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'id';
        $order = $paramData['order'] ? $paramData['order'] : 'desc';
        $keywords = $paramData['keywords'] ? $paramData['keywords'] : null;
        $whereArray = $paramData['whereArray'] ? $paramData['whereArray'] : null;
        $tagIds = $paramData['tagIds'] ? $paramData['tagIds'] : null;
        $tagNames = $paramData['tagNames'] ? $paramData['tagNames'] : null;
        
        if ($tagNames) {
            $tagNames = explode(',',$tagNames);
            foreach ($tagNames as $val) {
                $tagInfo = db('Tags')->where('title',$val)->find();
                if ($tagInfo) {
                    $tempTagIds[] = $tagInfo['id'];
                }
            }
            $tagIdsWhere['tags_id']  = ['in',$tempTagIds];
        }
        if ($tagIds) {
            $tagArray = explode(',',$tagIds);
            $tagIdsWhere['tags_id']  = ['in',$tagArray];
        }
        $count = db('TagsRel')->where($tagIdsWhere)->count();
        $tagInfo = null;
        if ($tagIds) {
            $tempTagIds = explode(',', $tagIds);
            if (count($tempTagIds) == 1) {
                $tagInfo = db('Tags')->where('id',$tagIds)->find();
            } else {
                return false;
            }
        }
        if ($tagNames) {
            $tempTagNames = explode(',', $tagNames);
            if (count($tempTagNames) == 1) {
                $tagInfo = db('Tags')->where('title',$tagNames)->find();
            } else {
                return false;
            }
        }
        
        if ($tagInfo) {
            $baseUrl = model('app\tag\model\Tags')->getItemInfo($tagInfo['id'])['url'];
        }
        $pagination_array = array(
            'base_url' => $baseUrl,
            'total_rows' => $count,
            'per_page' => $limit,
            'cur_page' => $page,
            'page_break' => '_'
        );
        $pagination = new Paginationm($pagination_array);
        return $pagination->create_links();
    }

}